/**
 * @module ChiVoxSDK
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import Utils from './utils';
import cfg from './config.json';

/**
 * ProgressBar类。<br/><br/>
 *
 * 录音时会显示的录音进度条。
 */
class ProgressBar {
    constructor() {
        this.__elements    = Utils.find_all(document, cfg.selector.panel.recordProgressBar) || [];
        this.__t_start     = 0;
        this.__interval_id = null;

        this.__curr_idx    = 0;
    }

    /**
     * 开始录音。
     *
     * @param {int} duration - 本次录音时长，单位：毫秒。
     * @param {int=} idx - 要控制的进度条序号。当页面内存在多个进度条时传入。
     */
    start(duration, idx) {
        idx  = idx || this.__curr_idx;
        if (idx < 0 || idx >= this.__elements.length) {
            console.warn(`cound not found the progressbar. index: ${idx}., all: ${this.__elements}`);
            return;
        }

        this.__curr_idx    = idx;
        let ele = this.__elements[this.__curr_idx];

        this.__t_start  = +new Date();
        ele.style.display = 'block';

        var ele_v = Utils.find(ele, '.value');

        this.__interval_id = setInterval(() => {
            var v = Math.ceil((+new Date() - this.__t_start) * 100  / duration);
            ele_v.style.width = (v >= 90) ? '100%' : `${v}%`;
        }, 100);
    }

    __clear() {
        this.__t_start  = 0;

        if (this.__interval_id != null)
            clearInterval(this.__interval_id);
        this.__interval_id = null;
    }

    /** 停止录音。 */
    stop() {
        this.__clear();

        let ele = this.__elements[this.__curr_idx];
        Utils.find(ele, '.value').style.width = '0%';
        this.hide();
    }

    /** 重置进度条状态。 */
    reset() {
        this.__clear();
        this.hide();
    }

    /** 隐藏进度条。 */
    hide() {
        for (var i = 0; i < this.__elements.length; i++) {
            let ele = this.__elements[i];
            ele.style.display = 'none';
        }
    }
}

export default ProgressBar
